//
//  HomeListModel.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/8.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import SwiftyJSON
struct ConsultaionHomeListModel {
    /// 标题
    var titleContent = ""
    /// 类型
    var typeName: String = "专业科普"
    /// 浏览量
    var pageView: String = String(arc4random() % 999)
    /// 图片
    var pageUrl: String = ""
    /// 收藏数量
    var collectionNumber: String = ""
    /// 回复数
    var replyQuantity = ""
    /// 顶部图片
    var homeImageUrl = ""
    /// html页面
    var htmlPage = ""
    /// 临时html页面
    var tempHtmlPage: Data = Data()
    /// 用户信息
    var userInfoModel = UserInfoModel()
    /// 评论信息
    var comments:[ConsultaionCommentModel] = []
}

extension ConsultaionHomeListModel {
    init(_ json: [String: Any]) {
        let jsonData = JSON(json).dictionaryValue
        pageUrl = jsonData["cover"]?.string ?? ""
        let htmlUrl = jsonData["url"]?.string ?? ""
        titleContent = htmlUrl
        let htmlPath = Bundle.main.path(forResource:htmlUrl, ofType: nil)
        tempHtmlPage = try! Data(contentsOf: URL(fileURLWithPath: htmlPath!))
        let comments = jsonData["comments"]?.array
        for comment in comments! {
            let comDic = comment.dictionary
            var comData = ConsultaionCommentModel()
            comData.userInfoModel.name = comDic!["userName"]?.string ?? "小甜甜"
            comData.commentContent = comDic!["comment"]?.string ?? ""
            self.comments.append(comData)
        }
        
    }
}
